﻿using System.Linq;
using Common.Views;
using Dto.ApiRequests.LeadForms;
using Dto.ApiRequests.MemberForms;
using Dto.ApiRequests.NewsLetter;
using Sanelib.Common.Extensions;
using Sanelib.Common.Helpers;
using Sanelib.DataView;
using Sanelib.Dto;

namespace Common.Service.Impl
{
    public class ModelService : IModelService
    {
        private readonly IViewRepository<MemberView> _memberViewRepository;
        private readonly IViewRepository<NewsLetterView> _newsLetterViewRepository;

        public ModelService(IViewRepository<MemberView> memberViewRepository, IViewRepository<NewsLetterView> newsLetterViewRepository)
        {
            _memberViewRepository = memberViewRepository;
            _newsLetterViewRepository = newsLetterViewRepository;
        }

        public bool CheckMemberRegistrationForm(RegistrationForm form, ErrorResult validationResult)
        {
            var userWithSameUsername = _memberViewRepository.GetByKey(Property.Of<MemberView>(entity => entity.Name), form.Name);
            if (userWithSameUsername != null)
                validationResult.AddError("A user with the same username already exists in the system.", string.Empty);

            var userWithSameEmail = _memberViewRepository.GetByKey(Property.Of<MemberView>(entity => entity.Address), form.Email);
            if (userWithSameEmail != null)
                validationResult.AddError("A user with the same email address already exists in the system.", string.Empty);

            if (string.IsNullOrWhiteSpace(form.Code) || form.Code == null)
                validationResult.AddError("Code", " should not be empty.");

            if (string.IsNullOrWhiteSpace(form.Name) || form.Name == null)
                validationResult.AddError("Name", " should not be empty.");

            if (form.DateOfBirth > SystemTime.Now() || form.DateOfBirth == null)
                validationResult.AddError("DateOfBirth", " is invalid.");

            if (string.IsNullOrWhiteSpace(form.Address) || form.Address == null)
                validationResult.AddError("Address", " should not be empty.");

            if (!string.IsNullOrWhiteSpace(form.Email) && form.Email.Length > 128)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (form.Email.IsEmpty())
            {
                validationResult.AddError("Email", "should not be empty");
            }
            else
            {
                if (form.Email.IsNotEmpty() && Formatter.EmailId(form.Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            if (string.IsNullOrWhiteSpace(form.Mobile) || form.Mobile == null)
                validationResult.AddError("MobileNumber", " should not be empty.");

            return validationResult.IsValid;
        }

        public bool CheckNewsLetterForm(AddNewsLetterForm form, ErrorResult validationResult)
        {
            var alradyAddedWithSameEmail = _newsLetterViewRepository.GetAllFor(Property.Of<LeadView>(x => x.Email), form.Email).SingleOrDefault();

            if (alradyAddedWithSameEmail != null)
                validationResult.AddError("Email ", " should not be duplicated. Already available in system.");

            if (!string.IsNullOrWhiteSpace(form.Email) && form.Email.Length > 128)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (form.Email.IsEmpty())
            {
                validationResult.AddError("Email", "should not be empty");
            }
            else
            {
                if (form.Email.IsNotEmpty() && Formatter.EmailId(form.Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            return validationResult.IsValid;
        }

        public bool CheckAnonymousLeadForm(AddAnonymousLeadForm form, ErrorResult validationResult)
        {

            if (!string.IsNullOrWhiteSpace(form.Name) && form.Name.Length > 128)
                validationResult.AddError("Name", " entered exceeds the maximum length 128");

            if (string.IsNullOrWhiteSpace(form.Name))
                validationResult.AddError("Name", "should not empty.");

            if (!string.IsNullOrWhiteSpace(form.CompanyName) && form.CompanyName.Length > 128)
                validationResult.AddError("Company Name", " entered exceeds the maximum length 128");

            if (string.IsNullOrWhiteSpace(form.CompanyName))
                validationResult.AddError("Company Name", " should not empty.");

            if (!string.IsNullOrWhiteSpace(form.Email) && form.Email.Length > 128)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (form.Email.IsEmpty())
                validationResult.AddError("Email", "should not be empty");
            else
            {
                if (form.Email.IsNotEmpty() && Formatter.EmailId(form.Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            if (string.IsNullOrWhiteSpace(form.Phone))
                validationResult.AddError("Mobile No.", " should not empty.");

            if (!string.IsNullOrWhiteSpace(form.Description) && form.Description.Length > 2048)
                validationResult.AddError("Description", " entered exceeds the maximum lenght 2048");

            if (string.IsNullOrWhiteSpace(form.Description))
                validationResult.AddError("Description", " should not empty.");

            return validationResult.IsValid;
        }
    }
}